<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/newsletter/om/Newsletter.php");



/**
 * BaseNewsletterForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseNewsletterForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * newsletter
     *
     * @var Newsletter 
     */
    protected $newsletter;


    /**
     * email
     *
     * @var mixed 
     */
    protected $email;


    /**
     * languages
     *
     * @var mixed 
     */
    protected $languages;


    /**
     * template
     *
     * @var mixed 
     */
    protected $template;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseNewsletterForm
     */
    public function __construct() {
        $this->newsletter = new Newsletter();
        $this->email = null;
        $this->languages = array();
        $this->template = null;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get Newsletter
     *
     * @return Newsletter 
     */
    public function getNewsletter() {
        return $this->newsletter;
    }


    /**
     * Set Newsletter
     *
     * @param Newsletter  newsletter
     */
    public function setNewsletter(Newsletter $newsletter) {
        $this->newsletter = $newsletter;
    }


    /**
     * Get Email
     *
     * @return mixed 
     */
    public function getEmail() {
        return $this->email;
    }


    /**
     * Set Email
     *
     * @param mixed  email
     */
    public function setEmail($email) {
        $this->email = $email;
    }


    /**
     * Get Languages
     *
     * @return mixed 
     */
    public function getLanguages() {
        return $this->languages;
    }


    /**
     * Set Languages
     *
     * @param mixed  languages
     */
    public function setLanguages($languages) {
        $this->languages = $languages;
    }


    /**
     * Get Template
     *
     * @return mixed 
     */
    public function getTemplate() {
        return $this->template;
    }


    /**
     * Set Template
     *
     * @param mixed  template
     */
    public function setTemplate($template) {
        $this->template = $template;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getNewsletter()->getSubject()) || strlen($this->getNewsletter()->getSubject()) == 0)
            $errors->add("newsletter->subject",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.subject", true)));


        if(!is_null($this->getNewsletter()->getSubject()) && strlen($this->getNewsletter()->getSubject()) > 0 && strlen($this->getNewsletter()->getSubject()) > 255)
            $errors->add("newsletter->subject",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.subject", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getNewsletter()->getBody()) || strlen($this->getNewsletter()->getBody()) == 0)
            $errors->add("newsletter->body",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.body", true)));


        if(!is_null($this->getEmail()) && strlen($this->getEmail()) > 0 && !ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $this->getEmail()))
            $errors->add("email",
                new ActionMessage("errors.email", true, new ActionMessage("prompt.email", true)));


        return $errors;
    }

}
?>